//
//  QRCodeScanningVC.m
//  SGQRCodeExample
//
//  Created by apple on 17/3/21.
//  Copyright © 2017年 JP_lee. All rights reserved.
//

#import "QRCodeScanningVC.h"
#import "MainViewController.h"

#import "CarDetailInfoView.h"

@interface QRCodeScanningVC ()
{
    CarDetailInfoView *carInfoV;
}
@end

@implementation QRCodeScanningVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    // 注册观察者
    [SGQRCodeNotificationCenter addObserver:self selector:@selector(SGQRCodeInformationFromeAibum:) name:SGQRCodeInformationFromeAibum object:nil];
    [SGQRCodeNotificationCenter addObserver:self selector:@selector(SGQRCodeInformationFromeScanning:) name:SGQRCodeInformationFromeScanning object:nil];
   
    carInfoV = [[[NSBundle mainBundle] loadNibNamed:@"CarDetailInfoView" owner:nil options:nil] lastObject];
    
    carInfoV.frame = CGRectMake(40, (KScreenHeight -  160)/2.0, KScreenWidth - 40 *2, 160);
    [carInfoV.sureBtnnH addTarget:self action:@selector(CarButtonAct:) forControlEvents:UIControlEventTouchUpInside];
    carInfoV.hidden = YES;
    [self.view addSubview:carInfoV];
    
    
}

- (void)SGQRCodeInformationFromeAibum:(NSNotification *)noti {
  

//    ScanSuccessJumpVC *jumpVC = [[ScanSuccessJumpVC alloc] init];
//    jumpVC.jump_URL = string;
//    [self.navigationController pushViewController:jumpVC animated:YES];
    
    
        MainViewController *mainVC = [[MainViewController alloc] init];
        [self.navigationController pushViewController:mainVC animated:YES];
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
     //开启会话，重新进行扫描
    [self.session startRunning];
}
- (void)SGQRCodeInformationFromeScanning:(NSNotification *)noti {
    
    
    SGQRCodeLog(@"noti - - %@", noti.object);
    
    NSString *notiString = noti.object;
    NSString *snNOStr = [notiString substringFromIndex:5];
  
    //将snNO保存至本地，以备后期用到
    [[NSUserDefaults standardUserDefaults] setObject:snNOStr forKey:@"user_snNO"];
//    __block QRCodeScanningVC *weakSelf = self;
     //扫码成功
    if ([notiString hasPrefix:@"snNo"]) {
        
        NSString *loginToken = GetUserLoginToken;
#pragma mark -- 调用签入车辆的接口 --
        [CoreWork signInWithloginToken:loginToken Block:^(id responsObject) {
           
            
#pragma mark -- 服务器报错  --
            if (![responsObject isKindOfClass:[NSDictionary class]]) {
                
                UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"服务器开小差了，请稍后重试" preferredStyle:UIAlertControllerStyleAlert];
                
                UIAlertAction *sure = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
                
                [alert addAction:sure];
                [self presentViewController:alert animated:YES completion:nil];
                
                return ;
                
            }
            
#pragma mark --  status = 0 --
            if (([responsObject isKindOfClass:[NSDictionary class]]) &&([[responsObject objectForKey:@"status"] integerValue] == 0)) {
                
                UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:[NSString stringWithFormat:@"%@",[responsObject objectForKey:@"message"]] preferredStyle:UIAlertControllerStyleAlert];
                
                UIAlertAction *sure = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
                
                [alert addAction:sure];
                [self presentViewController:alert animated:YES completion:nil];
                
                return ;
                
            }
            //未登录
            else   if (([responsObject isKindOfClass:[NSDictionary class]]) &&([[responsObject objectForKey:@"status"] integerValue] == -1)) {
                
                
                UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:[NSString stringWithFormat:@"%@",[responsObject objectForKey:@"message"]] preferredStyle:UIAlertControllerStyleAlert];
                
                UIAlertAction *sure = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
                
                [alert addAction:sure];
                [self presentViewController:alert animated:YES completion:nil];
                
                return ;
                
            }
            

            //扫描成功
            else   if (([responsObject isKindOfClass:[NSDictionary class]]) &&([[responsObject objectForKey:@"status"] integerValue] == 1)) {
              
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.01 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    dispatch_async(dispatch_get_main_queue(), ^{
                        
                       
                        carInfoV.hidden = NO;
                        
                        carInfoV.sureBtnnH.backgroundColor = [UIColor colorWithRed:0.337 green:0.682 blue:0.961 alpha:1.00];
                        carInfoV.carNumLabel.text = [NSString stringWithFormat:@"已成功签到车辆：%@",[[responsObject objectForKey:@"data"] objectForKey:@"carPlate"]];
                        carInfoV.carStyleLabel.text = [NSString stringWithFormat:@"车型：%@",[[responsObject objectForKey:@"data"] objectForKey:@"carType"]];
                        
                    });
                    
                });
               
                
                
            }
         
            
        }];
        

        
    }
    
    else
    {
        
        // 扫描失败
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"二维码有误，请重试" preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *sure = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
        
        [alert addAction:sure];
        [self presentViewController:alert animated:YES completion:nil];
        
        return ;
        
     
    }
    
    
   
}
-(void)CarButtonAct:(UIButton *)sender
{
    
    [carInfoV removeFromSuperview];
    if([[[UIDevice
          currentDevice] systemVersion] floatValue]>=8.0) {
        
        self.modalPresentationStyle=UIModalPresentationOverCurrentContext;
        
    }
    //设置参数判断是否已经登录
//    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"isLogin"];
    
    MainViewController *mainVC = [[MainViewController alloc] init];
    [self.navigationController pushViewController:mainVC animated:YES];
}
- (void)dealloc {
    SGQRCodeLog(@"QRCodeScanningVC - dealloc");
    [SGQRCodeNotificationCenter removeObserver:self];
}

@end
